<?php
class exercisetype extends model{
	
	/**
	 * 获取类型列表
	 * @param unknown_type $page
	 * @param unknown_type $pagesize
	 * @return multitype:unknown number
	 */
	public function getexercisetype($page, $pagesize)
	{
		$sql = "SELECT `exercise_type`.*, `subject`.* FROM `exercise_type` 
				LEFT JOIN `subject` ON `subject`.`sub_id` = `exercise_type`.`sub_id`
				ORDER BY `exercise_type`.`sub_id` ASC LIMIT ".( $page - 1 ) * $pagesize.", ".$pagesize."";
		$list = $this->fetchAll($sql);
		$total = $this->getTotal('exercise_type',NULL);
		$totalPage = ceil($total / $pagesize);
		//echo $totalPage;
		return array('page'=>$page,'list'=>$list,'total'=>$total,'totalPage'=>$totalPage);
	}
	
	/**
	 * 添加类型
	 * @param unknown_type $subid
	 * @param unknown_type $cont
	 * @return Ambigous <boolean, number>
	 */
	public function addexercisetype($subid, $cont, $score)
	{
		if($this->getdetailfromcont($subid, $cont))
			return -1;
		else{
			$sql = "INSERT INTO `exercise_type` (`et_cont`, `et_creattime`, `sub_id`, `et_score`) VALUES ('".$cont."', NOW(), '".$subid."', ".$score.")";
			return $this->insert($sql);
		}
	}
	
	/**
	 * 添加时判重用
	 * @param unknown_type $subid
	 * @param unknown_type $cont
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getdetailfromcont($subid, $cont)
	{
		$sql = "SELECT * FROM `exercise_type` WHERE `sub_id` = '".$subid."' AND `et_cont` = '".$cont."'";
		return $this->fetchAll($sql);
	}
	/**
	 * 修改类型
	 * @param unknown_type $id
	 * @param unknown_type $cont
	 * @return resource
	 */
	public function modexercisetype($id, $cont, $score)
	{
		$sql = "UPDATE `exercise_type`
				SET `et_cont` = '".$cont."',
					`et_score` = ".$score."
				WHERE `et_id` = '".$id."'";
		return $this->update($sql);
	}
	
	/**
	 * 删除类型
	 * @param unknown_type $id
	 * @return resource
	 */
	public function delexercise($id)
	{
		$sql = "DELETE FROM `exercise_type` WHERE `et_id` = '".$id."'";
		return $this->del($sql);
	}
	
	/**
	 * 获取科目下拉列表
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getsublist()
	{
		$sql = "SELECT * FROM `subject`";
		return $this->fetchAll($sql);
	}
	
	/**
	 * 获取题目
	 * @param unknown_type $id
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getetdetailbyid($id)
	{
		$sql = "SELECT `exercise_type`.*, `subject`.* FROM `exercise_type` 
				LEFT JOIN `subject` ON `subject`.`sub_id` = `exercise_type`.`sub_id`
				WHERE `exercise_type`.`et_id` = '".$id."'";
		return $this->fetchRow($sql);
	}
	
	/**
	 * 根据科目ID获取类型
	 * @param unknown_type $subjectID
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getTypelistBySubjectID($subjectID)
	{
		$sql = "SELECT * FROM `exercise_type` WHERE `sub_id` = '".$subjectID."' ";
		return $this->fetchAll($sql);
	}
	
	
	
	/**
	 * 删除题目类型
	 * @param unknown_type $id
	 * @return number
	 */
	public function delet($id)
	{
		$num = $this->getExerciseNumFromEt($id);
		//echo $num;
		if( $num > 0 ){
			return -1;
		}
		else{
			$sql = "DELETE FROM `exercise_type` WHERE `et_id` = '".$id."'";
			return $this->del($sql) ? 1 : 0;
			//return 1;
		}
	}
	/**
	 * 获取某科目下的题目类型
	 * @param unknown_type $sid
	 * @return Ambigous <boolean, multitype:>
	 */
	 public function getTypeBySubjectID($sid)
	 {
	 	$sql = "SELECT `et_id`,`et_cont` FROM `exercise_type` WHERE `sub_id`='".$sid."'";
	 	return $this->fetchAll($sql);
	 }
	/**
	 * 获取题目类型下的题目数目
	 * @param unknown_type $etid
	 * @return number
	 */
	public function getExerciseNumFromEt($etid){
		return $this->getTotal("exercise","`et_id`=".$etid);
	}
	
	/**
	 * 根据科目id题目类型
	 * @param unknown_type $subid
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getetfromsubid($subid)
	{
		$sql = "SELECT * FROM `exercise_type` WHERE `sub_id` = '".$subid."'";
		return $this->fetchAll($sql);
	}
	
}